import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const routes = [
  {
    path: '/login',
    component: () => import('@/views/login')
  },
  {
    path: '/home',
    component: () => import('@/views/home'),
    redirect: '/home/welcome',
    children: [
      {
        path: '/home/welcome',
        component: () => import('@/views/home/welcome')
      },
      {
        path: '/home/users',
        component: () => import('@/views/home/users')
      },
      {
        path: '/home/rights',
        component: () => import('@/views/home/power/rights')
      },
      {
        path: '/home/roles',
        component: () => import('@/views/home/power/roles')
      },
      {
        path: '/home/categories',
        component: () => import('@/views/home/goods/cate')
      },
      {
        path: '/home/params',
        component: () => import('@/views/home/goods/params')
      },
      {
        path: '/home/goods',
        component: () => import('@/views/home/goods/list')
      },
      {
        path: '/home/add',
        component: () => import('@/views/home/goods/list/add')
      },
      {
        path: '/home/orders',
        component: () => import('@/views/home/order/order')
      },
      {
        path: '/home/reports',
        component: () => import('@/views/home/report/report')
      }
    ]
  },
  {
    path: '/',
    redirect: '/login'
  }
]

const router = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// 暴露路由对象之前，要挂载路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else {
    const tokenStr = window.sessionStorage.getItem('token')
    if (tokenStr) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
